import { debug } from "util";

    // const data_demo = {
    //     "content": [
    //         ["5cda59c789f853397ef80653", "116.40", "39.90", "2019", "0", "001", "2018", "北京天使医院", 1, "110000000000"],
    //         ["5cda59c789f853397ef80654", "104.06", "30.67", "2019", "0", "002", "2019", "八一儿童医院", 2, "510000000000"],
    //         ["5cda59c789f853397ef80655", "116.90", "40", "2019", "0", "001", "2017", "北京市一医院", 3, "110000000000"],
    //         ["5cda59c789f853397ef80656", "115", "38", "2019", "0", "002", "2016", "北京市二医院", 4, "510000000000"],
    //         ["5cda59c789f853397ef80656", "115", "38", "2019", "0", "002", "2016", "北京市三医院", 4, "510000000000"]
    //     ],
    //     "count": 4,
    //     "header": [{
    //         "iden_code": "ID",
    //         "version": "2019-05-16 14:15:59"
    //     }, {
    //         "iden_code": "SMX",
    //         "version": "2019-05-16 14:15:59"
    //     }, {
    //         "iden_code": "SMY",
    //         "version": "2019-05-16 14:15:59"
    //     }, {
    //         "iden_code": "YEAR",
    //         "version": "2019-05-16 14:15:59"
    //     }, {
    //         "iden_code": "month",
    //         "version": "2019-05-16 14:15:59"
    //     }, {
    //         "id": "5cda576089f853397ef8064f",
    //         "iden_alias_code": "M",
    //         "iden_code": "B0010003",
    //         "iden_length": 255,
    //         "iden_name": "枚举测试",
    //         "iden_precision": 0,
    //         "iden_type": 4,
    //         "ident_version": "2018版",
    //         "identid": "5cd3fb9089f8c70c864bceec",
    //         "version": "2019-05-16 14:15:59"
    //     }, {
    //         "id": "5cda3e3389f853397ef8064d",
    //         "iden_code": "B0010026",
    //         "iden_length": 255,
    //         "iden_name": "日期测试",
    //         "iden_precision": 0,
    //         "iden_type": 3,
    //         "version": "2019-05-16 14:15:59"
    //     }, {
    //         "id": "5cda3e3389f853397ef8064e",
    //         "iden_alias_code": "NAME",
    //         "iden_code": "B0010024",
    //         "iden_length": 255,
    //         "iden_name": "文本测试",
    //         "iden_precision": 0,
    //         "iden_type": 1,
    //         "version": "2019-05-16 14:15:59"
    //     }, {
    //         "id": "5cda57cc89f853397ef80650",
    //         "iden_alias_code": "num",
    //         "iden_code": "B0010004",
    //         "iden_length": 8,
    //         "iden_name": "数字测试",
    //         "iden_precision": 2,
    //         "iden_type": 2,
    //         "iden_unit_name": "克",
    //         "version": "2019-05-16 14:15:59"
    //     }, {
    //         "id": "5cda580189f853397ef80651",
    //         "iden_alias_code": "QHCODE",
    //         "iden_code": "B0010005",
    //         "iden_length": 255,
    //         "iden_name": "区划代码",
    //         "iden_precision": 0,
    //         "iden_type": 1,
    //         "version": "2019-05-16 14:15:59"
    //     }],
    //     "pageNumber": 1,
    //     "pageSize": 10,
    //     "tableName": "T_MICRO_ZB21557806641454"
    // }


    //格式化表格数据
    var inittableData = function (data) {
        if (data.hasOwnProperty("content") && data.content.total>0) {
            let header = data.head;
            let content = data.content.records;
            let arr = [];
            for (let i = 0; i < content.length; i++) {
                let item = content[i];
                let obj = {
                    info:{
                        ID:item.ID,
                        MONTH:item.MONTH,
                        SMX:item.SMX,
                        SMY:item.SMY,
                        YEAR:item.YEAR,
                        ZZDM:item.ZZDM
                    }
                };
                for (let j = 0; j < header.length; j++) {
                    // let iden_code = header[j].idenCode;
                    // obj[iden_code] = item[iden_code];
                    let header_item = header[j];
                    obj[header_item.idenCode] = {
                        name: header_item.idenName || header_item.idenCode,
                        value: "",
                        idenType:header_item.idenType,
                        idenAliasCode:header_item.idenAliasCode
                    };
                    for(let k in item){
                        if(k==header_item.idenCode){
                            obj[header_item.idenCode].value = item[k];
                        }
                    }
                }
                arr.push(obj);
            }
            return arr;
        }
    }
    //格式化marker数据
    var initMarkerData = function(data){
        if (data.hasOwnProperty("content")) {
            let header = data.head;
            let content = data.content.records;
            let arr = [];
            for (let i = 0; i < content.length; i++) {
                let item = content[i];
                let obj = {
                    info:{
                        ID:item.ID,
                        MONTH:item.MONTH,
                        SMX:item.SMX,
                        SMY:item.SMY,
                        YEAR:item.YEAR
                    }
                };
                
                for (let j = 0; j < header.length; j++) {
                    let header_item = header[j];
                    obj[header_item.idenCode] = {
                        name: header_item.idenName || header_item.idenCode,
                        value: "",
                        idenType:header_item.idenType,
                        idenAliasCode:header_item.idenAliasCode
                    };
                    for(let k in item){
                        if(k==header_item.idenCode){
                            obj[header_item.idenCode].value = item[k];
                        }
                    }

                }
                arr.push(obj);
            }

            let pockdata = [];
            for (let i = 0; i < arr.length; i++) {
                pockdata.push({
                    id: arr[i].info.ID,
                    lon: arr[i].info.SMX,
                    lat: arr[i].info.SMY,
                    value: [arr[i].info.SMX, arr[i].info.SMY, 0],
                    data: arr[i]
                });
            }


            return pockdata;
        }
    }

    //格式化麻点(Marker)数据
    var initPockingData = function (data) {
        let pockdata = [];
        for (let i = 0; i < data.length; i++) {
            pockdata.push({
                id: data[i].id,
                name:data[i].name,
                lon: data[i].smx,
                lat: data[i].smy,
                value: [data[i].smx, data[i].smy, 0],
                qhdm:data[i].qhdm,
                jzwdm:data[i].jzwdm
            });
        }
        return pockdata;
    }
    //格式化从ES中查询的数据为麻点数据
    var initPockingData_ESSearch = function (data) {
        let Len = data.data;
        let Arr = [];
        for (let i = 0; i < Len.length; i++) {
            let dataobj = {
                TYPE: 'ESSearch'
            }
            let properties = Len[i]._source.properties;
            for (let j in properties) {
                dataobj[j] = {
                    name: j,
                    value: properties[j]
                }
            }
            Arr.push({
                data: dataobj,
                id: '',
                lat: Len[i]._source.location[1],
                lon: Len[i]._source.location[0],
                value: [Len[i]._source.location[0], Len[i]._source.location[1], 0]
            })
        }
        return Arr;
    }
    //格式化热力图数据
    var initHeatData = function (data,idenObj) {
        let val = "count";
        if(idenObj){//有传入指标对象
            if(idenObj.idenCode){
                val = "sum";
            }
        }
        let polydata = [];
        for (let i = 0; i < data.length; i++) {
            polydata.push([data[i].y, data[i].x,data[i][val]||0]);
        }
        return polydata;

    }
    //格式化网格图数据
    var initGridData = function (data,idenObj) {
        let val = "count";
        if(idenObj){//有传入指标对象
            if(idenObj.idenCode){
                val = "sum";
            }
        }
        let griddata = {
            griddata:[],
            maxdata:0,
            mindata:0,
            alldata:[]
        };
        for (let i = 0; i < data.length; i++) {
            griddata.griddata.push({
                geometry: {
                    type: 'Point',
                    coordinates: [data[i].x,data[i].y]
                },
                count: data[i][val]||0
            })
            griddata.alldata.push(data[i][val]||0)
        }
        griddata.maxdata = Math.max(...griddata.alldata);
        griddata.mindata = Math.min(...griddata.alldata);
        return griddata;
    }


    //格式化聚集图数据
    var initPolyData = function(data,idenObj){
        let polydata = {
            polydata:[],
            maxdata:0,
            mindata:0,
            alldata:[]
        };
        let val = "count";
        let name = "单位数量";
        if(idenObj){//有传入指标对象
            if(idenObj.idenCode){
                val = "sum";
                name = idenObj.idenName
            }
        }
        for (let i = 0; i < data.length; i++) {
            polydata.polydata.push({
                name: data[i].name,
                regionCode:data[i].regionCode,
                value: [data[i].x, data[i].y, data[i][val]||0],
                data:{}
            });
            polydata.polydata[i].data[name] = data[i][val]||0;
            polydata.alldata.push(data[i][val]||0)
        }
        polydata.maxdata = Math.max(...polydata.alldata);
        polydata.mindata = Math.min(...polydata.alldata);
        return polydata;
    }


    export default {
        // data_demo: data_demo,
        inittableData: inittableData,
        initPockingData: initPockingData,
        initPockingData_ESSearch: initPockingData_ESSearch,
        initHeatData: initHeatData,
        initMarkerData:initMarkerData,
        initPolyData:initPolyData,
        initGridData:initGridData
    };
